package com.hud.easynavi;

import android.os.Handler;
import android.os.Looper;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lucian on 17-4-1.
 */
public class AnimateControl{
    Timer tm;
    TimerTask timerTask;
    View view;
    boolean show;
    boolean isRunning = true;
    boolean isRepeatable = false;
    int interval = 500;

    public enum Speed{
        Fast,Slow
    }
    Speed blinkSpeed = Speed.Fast;

    int cnt = 9;
    AnimateCallback callback;
    Handler handler;

    public AnimateControl(View v, AnimateCallback cb)
    {
        tm = new Timer();
        callback = cb;
        view = v;
        handler = new Handler(Looper.myLooper());
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if(view!=null)
                {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(callback!=null){
                                callback.onStartAnimation(view);
                            }
                        }
                    });

                    while(isRunning){
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if(show){
                                    view.setVisibility(View.VISIBLE);
                                }else{
                                    view.setVisibility(View.INVISIBLE);
                                }
                            }
                        });
                        show = !show;
                        try {
                            if(blinkSpeed == Speed.Fast){
                                Thread.sleep(200);
                            }else if(blinkSpeed == Speed.Slow){
                                Thread.sleep(500);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if(!isRepeatable){
                            if(cnt-- < 0)
                                break;
                        }
                    }

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(callback!=null){
                                callback.onEndAnimation(view);
                            }
                        }
                    });
                }
            }
        };
    }

    public void start(){
        tm.schedule(timerTask,interval);
    }

    public AnimateControl setInterval(int t){
        interval = t;
        return this;
    }

    public AnimateControl setRepeat(boolean b){
        isRepeatable = b;
        return this;
    }

    public AnimateControl setCount(int c){
        cnt = c;
        return this;
    }

    public AnimateControl setSpeed(Speed sp){
        blinkSpeed = sp;
        return this;
    }

    public void stop(){
        tm.cancel();
        timerTask.cancel();
        isRunning = false;
        if(callback!=null){
            callback.onEndAnimation(view);
        }
    }
}
